package dtu.app.listadapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.iqengines.demo.R;
import com.iqengines.sdk.Utils;

import dtu.app.db.ProductDatabaseHelper;
import dtu.app.fragment.ProductListFragment;
import dtu.app.model.Product;

public class ProductArrayAdapter extends ArrayAdapter<Product> {

	private final LayoutInflater mInflater;
	private ProductListFragment mCallbackFragment;
	
	public ProductArrayAdapter(Context context, ProductListFragment callback) {
		super(context, android.R.layout.simple_list_item_2);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mCallbackFragment = callback;
	}
	
	public void setData(List<Product> data) {
		clear();
		if (data != null) {
			for (Product p : data) {
				add(p);
			}
		}
	}
	
    /**
     * Populate new items in the list.
     */
    @Override public View getView(int position, View convertView, ViewGroup parent) {
        View view;
 
        if (convertView == null) {
            view = mInflater.inflate(R.layout.product_item, parent, false);
        } else {
            view = convertView;
        }
        
        Product item = getItem(position);
        ((TextView)view.findViewById(R.id.product_name)).setText(item.getName());
        ((TextView)view.findViewById(R.id.product_total)).setText(Double.toString(item.getInStockPieces()));
        
        Button delete = (Button)view.findViewById(R.id.deleteButton);
        
        ImageButton small = (ImageButton)view.findViewById(R.id.product_add);
        ImageButton big = (ImageButton)view.findViewById(R.id.product_sub);
        
        String unit = item.getUnit();
        if (unit.equalsIgnoreCase("kg") || unit.equalsIgnoreCase("kilograms"))
        {
        	small.setImageResource(R.drawable.ic_weight1x);
        	big.setImageResource(R.drawable.ic_weight5x);
        } else if (unit.equalsIgnoreCase("l"))
        {
        	small.setImageResource(R.drawable.ic_cup_half);
        	big.setImageResource(R.drawable.ic_cup_full);
        } else if (unit.equalsIgnoreCase("pieces") || unit.equalsIgnoreCase("pcs"))
        {
        	small.setImageResource(R.drawable.ic_pieces1);
        	big.setImageResource(R.drawable.ic_pieces5);
        }
        
        small.setTag(position);
        big.setTag(position);
        delete.setTag(position);
        
        delete.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v)
        	{
        		Integer p = (Integer) v.getTag();
        		Product item = getItem(p);
        		
        		ProductDatabaseHelper db = new ProductDatabaseHelper(getContext());
        		db.deleteProduct(item, false);
        		
        		mCallbackFragment.updateList(item.getCategory());
        	}
        });
        
        small.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Integer p = (Integer) v.getTag();
				Product item = getItem(p);
				
				item.setInStockAmount(item.getInStockAmount() - item.getPiecesAmount());
				item.setInStockPieces(item.getInStockPieces() - item.getPiecesAmount());
								
				ProductDatabaseHelper db = new ProductDatabaseHelper(getContext());
				if (item.getInStockPieces() <= 0 || item.getInStockAmount() <= 0)
				{
					db.deleteProduct(item, false);
				}
				else
				{
					db.updateProduct(item, false);
				}
				mCallbackFragment.updateList(item.getCategory());
			}
		});
        
        big.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Integer p = (Integer) v.getTag();
				Product item = getItem(p);
				
				item.setInStockAmount(item.getInStockAmount() - (item.getPiecesAmount() * 5));
				item.setInStockPieces(item.getInStockPieces() - (item.getPiecesAmount() * 5));
				
				ProductDatabaseHelper db = new ProductDatabaseHelper(getContext());
				if (item.getInStockPieces() <= 0 || item.getInStockAmount() <= 0)
				{
					db.deleteProduct(item, false);
				}
				else
				{
					db.updateProduct(item, false);
				}
				mCallbackFragment.updateList(item.getCategory());
			}
		});
        
        InputStream is = null;
		try {
			is = getContext().getAssets().open(item.getImagePath());
		} catch (IOException e) {
			e.printStackTrace();
		}				
		Bitmap origBmp = BitmapFactory.decodeStream(is);
		Bitmap thumb = transformBitmapToThumb(origBmp);
        ((ImageView)view.findViewById(R.id.product_image)).setImageBitmap(thumb);
 
        return view;
    }
    
	private Bitmap transformBitmapToThumb(Bitmap origBmp) {
		int thumbSize = getContext().getResources().getDimensionPixelSize(R.dimen.thumb_size);
		return Utils.cropBitmap(origBmp, thumbSize);
	}

}
